//
//  JOTarBar.m
//  新浪微博模拟
//
//  Created by song on 15/3/27.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "JOTarBar.h"
#import "JOTaBarButton.h"


@interface JOTarBar()

@property(nonatomic ,weak) UIButton *selectBtn;


@property(nonatomic , weak) UIButton *plusBtn;

@property(nonatomic , strong) NSMutableArray *tabButtons;

@end

@implementation JOTarBar


-(NSMutableArray *)tabButtons
{
    if(_tabButtons ==nil)
    {
        _tabButtons = [NSMutableArray array];
    }
    return _tabButtons;
}

// 设置 tabbar 的 背景颜色
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        self.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar_background"]];
    }
    
    UIButton *plusBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    [plusBtn addTarget:self action:@selector(plusClick:) forControlEvents:UIControlEventTouchUpInside];
    plusBtn.bounds =  CGRectMake(0, 0, plusBtn.currentBackgroundImage.size.width, plusBtn.currentBackgroundImage.size.height);
    
    self.plusBtn = plusBtn;
    [self addSubview:plusBtn];

    
    return self;
}


-(void)addTabBarButtonWithItem:(UITabBarItem *)item
{
    JOTaBarButton *button = [[JOTaBarButton alloc] init];
    button.item =item;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:button];
    [self.tabButtons addObject:button];
    if(self.tabButtons.count == 1)
    {
        [self buttonClick:button];
    }
  
    
}

-(void)plusClick:(UIButton *)btn
{
    if([self.delegate respondsToSelector:@selector(pulsButtonClick)])
    {
        [self.delegate pulsButtonClick];
    }
}

-(void)buttonClick:(UIButton *)btn
{
    
    if([self.delegate respondsToSelector:@selector(tabBar:didSelectButtonFrom:to:)]){
        [self.delegate tabBar:self didSelectButtonFrom:self.selectBtn.tag to:btn.tag];
    }
    
    self.selectBtn.selected =NO;
    btn.selected =YES;
    self.selectBtn = btn;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    
    CGFloat h =  self.frame.size.height;
    CGFloat w = self.frame.size.width;
    self.plusBtn.center = CGPointMake(w * 0.5, h * 0.5);
    
    CGFloat buttonH = h;
    CGFloat buttonW = w /self.subviews.count;
    
    for(int index = 0 ; index<self.tabButtons.count ;index++)
    {
        UIButton *button  = self.tabButtons[index];
        CGFloat buttonY =0;
        CGFloat buttonX = index * buttonW;
        if(index>1)
        {
            buttonX =(index +1)*buttonW;
        }
       
        button.tag = index;
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        
    }
    
    
    
}
@end
